/*
 * Copyright (C) 2000-2008 Jens Nixdorf and various contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.mopore.jat.core.fadingmessage.calibration.visual;

import java.awt.Point;
import java.awt.Shape;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

public class VisualCalibtrationAdapter implements KeyListener, MouseListener, MouseMotionListener,
        MouseWheelListener {

    private final IScreenOperator screenOperator;

    private final ICalibtratable calibtratable;

	private double offSetX;

	private double offSetY;

    public VisualCalibtrationAdapter(IScreenOperator screenOperator, ICalibtratable cutable) {
        this.screenOperator = screenOperator;
        this.calibtratable = cutable;
    }

    public void mousePressed(MouseEvent evt) {

        boolean left = (evt.getButton() == MouseEvent.BUTTON1);
        if (left) {
        	Point currentPoint = evt.getPoint();
			CalibrationState state = calibtratable.getCurrentCalibtration();
        	Shape messageWindowShape = state.getMessageWindowShape();
    		boolean mouseOverWindow = messageWindowShape.contains( currentPoint );
        	if ( mouseOverWindow ){
	        	screenOperator.setOperating(true);
	        	
	        	double x = messageWindowShape.getBounds().getX();
	        	double y = messageWindowShape.getBounds().getY();
	        	
	        	offSetX = currentPoint.getX() - x;
				offSetY = currentPoint.getY() - y;
        	}
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
     */
    public void mouseDragged(MouseEvent evt) {

        if (screenOperator.isOperating()) {
        	Point currentPoint = evt.getPoint();
        	
        	double newX = currentPoint.getX() - offSetX;
        	double newY = currentPoint.getY() - offSetY;
        	
        	CalibrationState calibration = 
        		calibtratable.getCurrentCalibtration();
        	
        	calibration.applyMove( newX, newY );
        }
    }



	/*
     * (non-Javadoc)
     * 
     * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
     */
    public void mouseReleased(MouseEvent evt) {
        boolean left = (evt.getButton() == MouseEvent.BUTTON1);
        if (left) {
        	screenOperator.setOperating(false);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
     */
    public void keyPressed(KeyEvent e) {
    	
        boolean escape = (e.getKeyCode() == KeyEvent.VK_ESCAPE);
        if (escape) {
        	calibtratable.finishCalibration();
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
     */
    public void keyReleased(KeyEvent e) {
    	// adapter dummy 
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
     */
    public void keyTyped(KeyEvent e) {
    	// adapter dummy    	
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
     */
    public void mouseClicked(MouseEvent e) {
    	// adapter dummy    	
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
     */
    public void mouseEntered(MouseEvent e) {
    	// adapter dummy    	
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
     */
    public void mouseExited(MouseEvent e) {
    	// adapter dummy    	
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
     */
    public void mouseMoved(MouseEvent e) {
    	
    	Point point = e.getPoint();
    	CalibrationState state = calibtratable.getCurrentCalibtration();
    	Shape messageWindowShape = state.getMessageWindowShape();
		boolean mouseOverWindow = messageWindowShape.contains( point );
    	state.setMouseOver( mouseOverWindow );
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
     */
    public void mouseWheelMoved(MouseWheelEvent e) {
    	// adapter dummy
    }
    

}
